<?php
/*
* php数据缓存
* by JJ
× time 12.03.10
*/
class MCache {
	
	private $CacheData;//数据源
	private $CacheName;//缓存变量名称
	private $CacheFile;//缓存地址(以\结束)	
	private $CacheFileName;//缓存文件名
	private $CacheSuffix;//后缀(默认PHP)	
	private $CachePath;//路径
	
	public function __construct(){
	}
	
	//初始化数据
	public function init($CacheData="",$CacheName="",$CacheFile="",$CacheFileName="noname",$Suffix=".php"){
		$this->CacheData = $CacheData;
		$this->CacheName = $CacheName;
		$this->CacheFile = $CacheFile;	
		$this->CacheFileName = $CacheFileName;	
		$this->Suffix = $Suffix;	
		$this->CachePath = $CacheFile.$CacheFileName.$Suffix;
	}
	
	//数组形式缓存
	public function ArrayCache(){
		$content = "<?php\r\n//缓存文件由jcms自动生成\r\n//生成时间:".date('Y-m-d H:i:s')."\r\nif (!defined('THINK_PATH')) exit();\r\n";
		$content .= "global $".$this->CacheName.";\r\n";
		$content .= "$".$this->CacheName." = ";		
		$content .= var_export($this->CacheData,true);
		$content .= ";\r\n?>";
		$r=@chmod($file,0777);
		$hand=file_put_contents($this->CachePath,$content);
		if($hand){
			return true;
		}else{
			return false;	
		}
	}
	
	//城市缓存
	public function JsCache(){
		$content = "var ".$this->CacheName."=".$this->CacheData.";";
		$r=@chmod($file,0777);
		$hand=file_put_contents($this->CachePath,$content);
		if($hand){
			return true;
		}else{
			return false;	
		}
	}	
	
	public function Cache(){
		$r=@chmod($file,0777);
		$hand=file_put_contents($this->CachePath,$this->CacheData);
		if($hand){
			return true;
		}else{
			return false;	
		}		
	}
	
	public function GetCache($file){
		$path = _HOME_CACHE_.$file.".cache.php";
		import("ORG.Io.Dir");
		if(file_exists($path)){
			require_once($path);
		}
	}
	
	/*
	*	url缓存
	*	arg 文件名 $a $b
	*/
	public function UrlCache($file,$arr){
		$content = "<?php\r\n//缓存文件由jcms自动生成\r\n//生成时间:".date('Y-m-d H:i:s')."\r\nif (!defined('THINK_PATH')) exit();\r\n";
		$content .= "$".$this->CacheName." = ";		
		$content .= var_export($this->CacheData,true);
		$content .= ";\r\n";
		$content .= "\$config['URL_ROUTE_RULES'] = array_merge(\$".$this->CacheName.",\$config['URL_ROUTE_RULES']);\r\n";
		$content .= "?>";
		$content = str_replace("\\\\","\\",$content);
		$r=@chmod($file,0777);
		$hand=file_put_contents($this->CachePath,$content);
		if($hand){
			return true;
		}else{
			return false;	
		}
	}
	
	/*
	*	系统默认
	*/
	public static function set($key,$value,$path=ROOT_PATH_ADMIN_CACHE){
		if(empty($key)||empty($value))return false;
		F($key,$value,$path);	
	}
	
	/*
	*	系统默认
	*/
	public static function get($key,$path=ROOT_PATH_ADMIN_CACHE){
		return F($key,'',$path);
	}
	
	public static function del($key,$path=ROOT_PATH_ADMIN_CACHE){
		import("ORG.Io.Dir");
		$Dir = new Dir($path.$key);
		if(is_dir($path.$key)){
			return $Dir->delDir($path.$key);
		}
	}
	
	/*
	*	清除缓存
	*/
	public static function clearCache(){
		$path = getcwd()."\\Temp\\";
		if(!is_dir($path) == false){
			delDirAndFile($path,true);
		}
	} 
	
}
?>